package com.csw.flink.core

import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

object Demo04SubmitConsumer {

  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    //创建配置文件对象
    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")
//    properties.setProperty("group.id", "csw2")
//    properties.setProperty("auto.offset.reset", "earliest") //earliest：读取所有数据 latest：读取最新数据
    /**
      * 创建kafka消费者
      */
    val consumer: FlinkKafkaConsumer[String] = new FlinkKafkaConsumer[String]("student1",new SimpleStringSchema(),properties)

    consumer.setStartFromEarliest()

    val kafkaDS: DataStream[String] = env.addSource(consumer)

    val countDS: DataStream[(String, Int)] = kafkaDS.map(_.split(",")(4)).map((_,1)).keyBy(_._1).sum(1)

    countDS.print()

    env.execute()

  }
}
